package com.th.supcom.pds.impl.service.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import net.carefx.fc.framework.web.gwt.util.FcUtils;
import net.carefx.framework.exception.BusinessException;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.InpPhamApplyPatInfo;
import com.th.supcom.pds.domain.vo.PtsPaiAdtLogVo;
import com.th.supcom.pds.impl.common.PdsConstants;
import com.th.supcom.pds.impl.common.enums.ArreageFlagEnum;
import com.th.supcom.pds.impl.common.enums.TransferActionEnum;
import com.th.supcom.pds.impl.common.utils.ExceptionThrowHelper;
import com.th.supcom.pds.medicare.api.AdapterPTSPatientInfoService;
import com.th.supcom.pts.external.patient.api.query.in.PtsExtInPatientAdtQueryService;
import com.th.supcom.pts.external.patient.api.query.in.PtsExtInPatientVisitQueryService;
import com.th.supcom.pts.external.patient.domain.in.PtsExtInPatientVisitInfo;
import com.th.supcom.pts.external.patient.domain.in.PtsExtPatientAdtLog;

/**
 * @author yzhang 调用PTS外部接口
 */
public class AdapterPTSPatientInfoServiceImpl implements AdapterPTSPatientInfoService
{
    /** 日志记录器. */
    private static final Logger s_LOGGER = LoggerFactory.getLogger (AdapterPTSPatientInfoServiceImpl.class);
    private PtsExtInPatientVisitQueryService m_ptsExtInPatientVisitQueryService;
    private PtsExtInPatientAdtQueryService m_ptsExtInPatientAdtQueryService;

    @Override
    public List <InpPhamApplyPatInfo> findPatiInfoByPatientId (Long patientId)
    {
        s_LOGGER.info ("调用findPatiInfoByPatientId (patientId = 【{}】", new Object[]
        { patientId });

        try
        {
            List <PtsExtInPatientVisitInfo> result = m_ptsExtInPatientVisitQueryService.findSpecifiedInPatientVisitList (patientId.toString ());
            Collections.sort (result, new Comparator <Object> ()
            {
                @Override
                public int compare (Object o1, Object o2)
                {
                    return (int) (((PtsExtInPatientVisitInfo) o2).getVisitId () - ((PtsExtInPatientVisitInfo) o1).getVisitId ());
                }
            });

            return convertPtsToPdsPatient (result);
        }
        catch (Exception e)
        {
            s_LOGGER.error ("查询pts患者信息异常！" + e.getLocalizedMessage ());
            throw new BusinessException ("查询pts患者信息异常！" + PdsConstants.DEFALUT_HTML_WRAP, e);
        }

    }

    @SuppressWarnings ("deprecation")
    @Override
    public List <InpPhamApplyPatInfo> findPatiInfoByPatiVisitId (Long patiVisitId)
    {
        s_LOGGER.info ("调用findPatiInfoByPatientId (PatiVisitId = 【{}】", new Object[]
        { patiVisitId });
        try
        {
            PtsExtInPatientVisitInfo ptsExtInPatientVisitInfo = new PtsExtInPatientVisitInfo ();
            ptsExtInPatientVisitInfo.setPaiVisitId (patiVisitId);
            List <PtsExtInPatientVisitInfo> result = m_ptsExtInPatientVisitQueryService.findInPatientVisitList (ptsExtInPatientVisitInfo);
            return convertPtsToPdsPatient (result);
        }
        catch (Exception e)
        {
            s_LOGGER.error ("查询pts患者信息异常！" + e.getLocalizedMessage ());
            throw new BusinessException ("查询pts患者信息异常！" + PdsConstants.DEFALUT_HTML_WRAP, e);
        }

    }

    @Override
    public List <PtsPaiAdtLogVo> findPaiAdtLogVosById (Long paiVisitId)
    {
        s_LOGGER.info ("调用findPaiAdtLogVosById (paiVisitId = 【{}】", new Object[]
        { paiVisitId });
        try
        {
            List <PtsExtPatientAdtLog> paiAdtLogs = m_ptsExtInPatientAdtQueryService.findInpPatAllAdtLog (paiVisitId);
            ExceptionThrowHelper.throwBusinessException (CollectionUtils.isEmpty (paiAdtLogs), "查询患者病区时出现异常，请联系计算机中心！");
            @SuppressWarnings ("unused")
            List <PtsPaiAdtLogVo> adtLogVos = convertAdtLogVos (paiAdtLogs);
            List <PtsPaiAdtLogVo> adtLogMockVos = getMockAdtLog ();
            return adtLogMockVos;
        }
        catch (Exception e)
        {
            s_LOGGER.error ("查询pts患者流转信息异常！" + e.getLocalizedMessage ());
            throw new BusinessException ("查询pts患者流转信息异常！", e);
        }
    }

    @SuppressWarnings ("deprecation")
    private List <PtsPaiAdtLogVo> getMockAdtLog ()
    {
        List <PtsPaiAdtLogVo> paiAdtLogVos = new ArrayList <PtsPaiAdtLogVo> ();
        PtsPaiAdtLogVo paiAdtLogVO = new PtsPaiAdtLogVo ();
        paiAdtLogVO.setWard ("0106010601");
        paiAdtLogVO.setWardName ("呼吸内科病房");
        paiAdtLogVO.setNursingUnit ("0102270504");
        paiAdtLogVO.setNursingUnitName ("呼吸内科护理单元");
        paiAdtLogVO.setBeginDate (new Date (2014 - 1900, 10, 20));
        paiAdtLogVO.setEndDate (new Date (2014 - 1900, 11, 1));
        paiAdtLogVos.add (paiAdtLogVO);
        paiAdtLogVO = new PtsPaiAdtLogVo ();
        paiAdtLogVO.setWard ("0106010401");
        paiAdtLogVO.setWardName ("消化内科病房");
        paiAdtLogVO.setNursingUnit ("0102270501");
        paiAdtLogVO.setNursingUnitName ("消化内科护理单元");
        paiAdtLogVO.setBeginDate (new Date (2014 - 1900, 11, 1));
        paiAdtLogVO.setEndDate (new Date ());
        paiAdtLogVos.add (paiAdtLogVO);
        return paiAdtLogVos;
    }

    private List <PtsPaiAdtLogVo> convertAdtLogVos (List <PtsExtPatientAdtLog> paiAdtLogs)
    {
        List <PtsPaiAdtLogVo> paiAdtLogVos = new ArrayList <PtsPaiAdtLogVo> ();
        for (int i = 0; i < paiAdtLogs.size (); i++)
        {
            String transferType = paiAdtLogs.get (i).getTransferType ();
            if (TransferActionEnum.NewIn.getValue ().equals (transferType) || TransferActionEnum.TransferInBed.getValue ().equals (transferType))
            {
                PtsPaiAdtLogVo adtLogVo = new PtsPaiAdtLogVo ();
                adtLogVo.setPaiVisitId (paiAdtLogs.get (i).getPaiVisitId ());
                adtLogVo.setPaiTransferId (paiAdtLogs.get (i).getPaiTransferId ());
                adtLogVo.setBedCode (paiAdtLogs.get (i).getBedCode ());
                adtLogVo.setBedId (paiAdtLogs.get (i).getBedId ());
                adtLogVo.setTransferType (paiAdtLogs.get (i).getTransferType ());
                adtLogVo.setTranferName (paiAdtLogs.get (i).getTranferName ());
                adtLogVo.setTransferDate (paiAdtLogs.get (i).getTransferDate ());
                adtLogVo.setDepartmentCode (paiAdtLogs.get (i).getDept ());
                adtLogVo.setDepartmentName (paiAdtLogs.get (i).getDeptName ());
                adtLogVo.setNursingUnit (paiAdtLogs.get (i).getNursingUnit ());
                adtLogVo.setNursingUnitName (paiAdtLogs.get (i).getNursingUnitName ());
                adtLogVo.setWard (paiAdtLogs.get (i).getWard ());
                adtLogVo.setWardName (paiAdtLogs.get (i).getWardName ());
                adtLogVo.setBeginDate (paiAdtLogs.get (i).getTransferDate ());
                paiAdtLogVos.add (adtLogVo);
            }
            if (TransferActionEnum.TransferOut.getValue ().equals (transferType) || TransferActionEnum.Discharge.getValue ().equals (transferType))
            {
                paiAdtLogVos.get (paiAdtLogVos.size () - 1).setEndDate (paiAdtLogs.get (i).getTransferDate ());
            }
            if (i == paiAdtLogs.size () - 1 && !TransferActionEnum.Discharge.getValue ().equals (transferType))
            {
                paiAdtLogVos.get (paiAdtLogVos.size () - 1).setEndDate (FcUtils.getCurrentServerDateTimeForClient ());
            }
        }
        return paiAdtLogVos;
    }

    @Override
    public List <InpPhamApplyPatInfo> obtainPatBasicInfo (String nurseUnitCode)
    {
        PtsExtInPatientVisitInfo patientVisitInfo = new PtsExtInPatientVisitInfo ();
        patientVisitInfo.setCurrentNursingUnit (nurseUnitCode);
        List <PtsExtInPatientVisitInfo> patientVisitInfos = m_ptsExtInPatientVisitQueryService.findInPatientVisitList (patientVisitInfo);
        return convertPtsToPdsPatient (patientVisitInfos);
    }

    @SuppressWarnings ("deprecation")
    private List <InpPhamApplyPatInfo> convertPtsToPdsPatient (List <PtsExtInPatientVisitInfo> patientVisitInfos)
    {
        List <InpPhamApplyPatInfo> phamApplyPatInfos = new ArrayList <InpPhamApplyPatInfo> ();
        for (PtsExtInPatientVisitInfo patientVisitInfo : patientVisitInfos)
        {
            if (null == patientVisitInfo.getBedCode ())
            {
                continue;
            }
            InpPhamApplyPatInfo inpPhamApplyPatInfo = new InpPhamApplyPatInfo ();
            inpPhamApplyPatInfo.setVisitId (patientVisitInfo.getVisitId ());
            inpPhamApplyPatInfo.setBedNo (patientVisitInfo.getBedCode ());
            inpPhamApplyPatInfo.setDischargeDeptDate (patientVisitInfo.getDischargeDeptDate ());
            inpPhamApplyPatInfo.setNewInDeptDate (patientVisitInfo.getNewInDeptDate ());
            inpPhamApplyPatInfo.setNewInDeptName (patientVisitInfo.getNewInDeptName ());
            inpPhamApplyPatInfo.setChargeType (patientVisitInfo.getCostType ());
            inpPhamApplyPatInfo.setGender (patientVisitInfo.getPatientGender ());
            inpPhamApplyPatInfo.setPaiVisitId (patientVisitInfo.getPaiVisitId ());
            inpPhamApplyPatInfo.setPatientId (patientVisitInfo.getPatientId ());
            inpPhamApplyPatInfo.setPatName (patientVisitInfo.getPatientName ());
            inpPhamApplyPatInfo.setInFlag (patientVisitInfo.getInFlag ());
            inpPhamApplyPatInfo.setArreageDispense (String.valueOf (ArreageFlagEnum.YES.getEnumCode ()));
            phamApplyPatInfos.add (inpPhamApplyPatInfo);
        }
        return phamApplyPatInfos;
    }

    public void setPtsExtInPatientVisitQueryService (PtsExtInPatientVisitQueryService ptsExtInPatientVisitQueryService)
    {
        m_ptsExtInPatientVisitQueryService = ptsExtInPatientVisitQueryService;
    }

    public void setPtsExtInPatientAdtQueryService (PtsExtInPatientAdtQueryService ptsExtInPatientAdtQueryService)
    {
        m_ptsExtInPatientAdtQueryService = ptsExtInPatientAdtQueryService;
    }

}
